Repository navigation
The run file is the module __main__ (P37) - #49
Merged
Merged
Conversation
Run, the REPL, a replay and the visualizer run the file in a fresh module installed as sys.modules["__main__"], so "import __main__; __main__.x" sees its variables as in CPython. A trace and a replay restore the session's module afterwards; a traced REPL input stays a dry run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
As in
python3 main.py, the run file is now the modulesys.modules["__main__"]:What each part of glpython does:
__main__, runs the file in its namespace and installs it assys.modules["__main__"].__name__ == "__main__"stays true, soif __name__ == "__main__":runs.y = 5→__main__.yis 5, and__main__.z = 7→zis 7. Before any Run, the REPL's own namespace is__main__, as in Python's REPL.python -m doctestdoes, so theif __name__ == "__main__":block doesn't run there.__main__is the module of the Tests' Run.__main__while it runs, and the session's__main__is back afterwards.__main__and remains a dry run:__main__.y = 99changes nothing in the session.__main__of its own and restores the session's at the end.sys._getframe(1).f_globalsfrom a function in another file still reads the caller's variables after ▶ and in the REPL, as the course uses it.One fix in the visualizer was needed: the fallback that runs an
importdropped every name starting with__, soimport __main__never bound the name. It now skips only__builtins__and dunder names the statement didn't change.Checks
scripts/check-runtime-main.py, added totest:runtime:__main__.x,print(__main__),__main__ is sys.modules["__main__"], theif __name__block,_getframe(1).f_globalsand__main__fromhilfe.py).__main__.x,__main__.yaftery = 5,zafter__main__.z = 7,hilfe.lies("y")with_getframe. The namespace is the module's.import __main__; __main__.aworks.__main__is back afterwards.__main__.yas a dry run.scripts/check-runtime-replay.py: its fresh-worker case now starts from a__main__module, as the worker does.scripts/check-console-browser.mjs, in Pyodide: a Run prints__main__.xand runs theif __name__block, and__main__.y * 10in the REPL gives 50.lint,test:runtime,test:protocolandtest:consolepass.How it goes live
maindeploys the embed, so the courses get it straight away.v0.1.24covers both.Manual tests (desktop, and iPad with an external keyboard)
2, then<module '__main__' from '/main.py'>, thenals Programm:y = 5then__main__.ygives5, and__main__.z = 7thenzgives7.__main__.xshows2. Afterwards,__main__.yin the REPL still gives5.sys._getframe(1).f_globalsfrom the library file) passes as before.if __name__ == "__main__":block: the doctests pass, and the block doesn't run during the doctests.Follow-ups (not fixed)
sys._getframe(1).f_globalsin the visualizer (unchanged, the same onmain): a function from another file is real Python there, and its caller's frame is the visualizer's interpreter, sof_globalsholds the runtime's names (KeyError: 'x'). After ▶ and in the REPL it works.import __main__from the library now works everywhere, the visualizer included, if the course ever wants a way that works there too.__file__in the visualizer ismain.pyand in a Run it is/main.py(pre-existing). Soprint(__main__)differs between the two.sys.modules(e.g.sys.modules["main"]), aspython -m doctestdoes. Not needed so far.🤖 Generated with Claude Code